#define _GNU_SOURCE
#include <stdlib.h>
#include <dlfcn.h>
#include <stdio.h>

static void *(*lt_malloc) (size_t size) = NULL;
static void (*lt_free)(void *ptr) = NULL;
 
__attribute__ ((constructor))
static void init_mem_func (void) 
{
	lt_malloc = (void *(*)(size_t))dlsym(RTLD_NEXT, "malloc");
    lt_free = (void (*)(void *))dlsym(RTLD_NEXT, "free");
}

void * __real_malloc(size_t size) {
     printf("real malloc: %zu\n", size);
    if(lt_malloc == NULL){
		init_mem_func();
	}
	void *ptr = lt_malloc(size);
    return ptr;
}

void __real_free(void *ptr) {
    printf("real free: %p\n", ptr);
    if(!ptr) return;
    if(lt_free == NULL) {
		init_mem_func();
    }
    lt_free(ptr);
}

void * __wrap_malloc(size_t size) {
    printf("my malloc: %zu\n", size);
    return __real_malloc(size);
}

void __wrap_free(void *ptr) {
    printf("my free: %p\n", ptr);
    __real_free(ptr);
}